package com.glebusheg.ui 
{
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	/**
	 * ...
	 * @author Gleb Volkov
	 */
	public class ScrollArea extends Sprite
	{
		private var _indentX:int = 0;
		private var _indentY:int = 0;
		private var _offsetX:int = 5;
		private var _offsetY:int = 5;
		
		private var _width:Number;
		private var _height:Number;
		
		private var _scrollAreaHeight:Number;
		private var _scrollArea:Sprite;
		
		private var _controls:Vector.<AppItemControl>;
		
		public function ScrollArea(width:Number, height:Number) 
		{
			_height = _scrollAreaHeight = height;
			_width = width;
			
			mouseEnabled = false;
			scrollRect = new Rectangle(0, 0, width, height);
			
			_scrollArea = new Sprite();
			_scrollArea.mouseEnabled = false;
			addChild(_scrollArea);
		}
		
		public function addControls(num:int):void
		{
			_controls = new Vector.<AppItemControl>();
			
			var numOfColumns:int = 4;
			var control:AppItemControl;
			for (var i:int = 0; i < num; i++) 
			{
				control = new AppItemControl();
				control.x = _indentX + (i % numOfColumns) * (control.width + _offsetX);
				control.y = _indentY + int(i / numOfColumns) * (control.height + _offsetY);
				_scrollArea.addChild(control);
				_controls.push(control);
			}
		}
		
		public function destroy():void 
		{
			if (_scrollArea)
			{
				if (_controls && _controls.length > 0)
				{
					for each (var control:AppItemControl in _controls) 
					{
						control.destroy();
						_scrollArea.removeChild(control);
					}
					_controls.length = 0;
				}
				
				removeChild(_scrollArea);
				_scrollArea = null;
			}
		}
		
		public function scroll(value:Number):void
		{
			_scrollArea.y = value;
		}
		
		public function scrollCheck():Boolean
		{
			return _scrollArea.height > _scrollAreaHeight;
		}
		
		public function setupItems(data:Vector.<String>):void 
		{
			var control:AppItemControl;
			for (var i:int = 0; i < data.length; i++) 
			{
				_controls[i].setupItem(data[i]);
			}
		}
		
		public function get scrollAmount():int
		{
			return _scrollAreaHeight - _scrollArea.height;
		}
	}
}